<?php

// $Id$


require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('country.class.php');


class Country_DBC extends Country implements I_DB_Storable {
	public function load_by_id(DB_Connection $dbc, $id) {
		$ret = false;

		$sql = "select id, ".
						"code, ".
						"name ".
					"from countries where id = $id";

		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();
			$this->set_id($id);
			$this->set_code(stripslashes($rez_obj->CODE));
			$this->set_name(stripslashes($rez_obj->NAME));

			$ret = $id;
		}

		return $ret;
	}


	public function save(DB_Connection $dbc) {
		$ret = true;

		// fake method

		return $ret;
	}


	public static function load_countries_ol_map(DB_Connection $dbc) {
		$sql = "select id, name from countries order by name asc";
		$rez = $dbc->execute($sql);

		$ret = array();
		$ids = array();

		while($rez_obj = $rez->fetch_object()) {
			array_push($ids, $rez_obj->ID);
			$ret['ol_map'][$rez_obj->ID] = $rez_obj->NAME;
		}

		if ($ids) {
			$ret['ids'] = $ids;
		}


		return $ret;
	}


	public function load_by_code(DB_Connection $dbc, $code) {
		$ret = false;
		$code = addslashes($code);

		$sql = "select id from countries where code = '$code'";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();
			$ret = $this->load_by_id($dbc, $rez_obj->ID);
		}


		return $ret;
	}
}